﻿QUnit.testStart(function () {
    var itinerary = new OffersNS.Itinerary();
    itinerary.add({
        city: "Atina",
        description: "Walking"
    });

    itinerary.add({
        city: "Drama",
        description: "Resting"
    });

    excursion = new OffersNS.Excursion({
        id: 10,
        title: "Excursion in Greece",
        startDate: "03.03.2013",
        endDate: "12.09.2013",
        price: 150,
        thumbnailUrl: "images/holiday-albena.jpg",
        imageUrl: "images/albena-big.jpg",
        description: "some description",
        transportType: OffersNS.TransportType.air,
        itinerary: itinerary,
    });

    holiday = new OffersNS.PackageHoliday({
        id: 6,
        title: "Holliday in sofia",
        startDate: "30.01.2009",
        endDate: "12.09.2012",
        price: 59,
        thumbnailUrl: "images/holiday-small.jpg",
        imageUrl: "images/holiday-big.jpg",
        description: "some description",
        transportType: OffersNS.TransportType.land
    });

    cruise1 = new OffersNS.Cruise({
        "type": "cruise",
        "id": 2,
        "title": "Cruise on white sea",
        "startDate": "22.12.2012",
        "endDate": "22.12.2013",
        "price": 12,
        "thumbnailUrl": "images/holiday-small.jpg",
        "imageUrl": "images/holiday-big.jpg",
        "waterbody": "greeece",
        "description": "bla bla",
    })

    cruise2 = new OffersNS.Cruise({
        "type": "cruise",
        "id": 3,
        "title": "Cruise on black sea",
        "startDate": "12.03.2009",
        "endDate": "12.09.2012",
        "price": 120,
        "thumbnailUrl": "images/holiday-albena.jpg",
        "imageUrl": "images/albena-big.jpg",
        "waterbody": "Albena (Bulgaria)",
        "description": "Albena",
    })

    slider = new SliderControl.Slider('#qunit-fixture');
    slider.addOffer(excursion);
    slider.addOffer(holiday);
    slider.addOffer(cruise1);
    slider.addOffer(cruise2);
})

var slider;
var excursion;
var holiday;
var cruise1;
var cruise2;

test("Add offer to slider", function () {
    equal(4, slider.offers.length, "Offers count");
    deepEqual(excursion, slider.offers[0], "Equal first offer");
    deepEqual(holiday, slider.offers[1], "Equal second offer");
    deepEqual(cruise1, slider.offers[2], "Equal third offer");
    deepEqual(cruise2, slider.offers[3], "Equal fourth offer");
});

test("Add offer to slider", function () {
    equal(4, slider.offers.length, "Offers count");
    deepEqual(excursion, slider.offers[0], "Equal first offer");
    deepEqual(holiday, slider.offers[1], "Equal second offer");
    deepEqual(cruise1, slider.offers[2], "Equal third offer");
    deepEqual(cruise2, slider.offers[3], "Equal fourth offer");
});


test("Slider render", function () {
    var fixture = document.getElementById("qunit-fixture");
    slider.render();

    var buttons = fixture.querySelectorAll('button');

    equal(buttons[0].id, 'slider-previous-arrow', 'Previous arrow');
    equal(buttons[1].id, 'slider-next-arrow', 'Next arrow');
});

test("Slider rotation (next image)", function () {
    var fixture = document.getElementById("qunit-fixture");
    slider.render();
    equal(slider.offers[0], slider.offers[slider.currentOfferIndex]);
    equal(excursion, slider.offers[slider.currentOfferIndex]);

    slider.nextOffer();
    equal(slider.offers[1], slider.offers[slider.currentOfferIndex]);
    equal(holiday, slider.offers[slider.currentOfferIndex]);

    slider.nextOffer();
    equal(slider.offers[2], slider.offers[slider.currentOfferIndex]);
    equal(cruise1, slider.offers[slider.currentOfferIndex]);

    slider.nextOffer();
    equal(slider.offers[3], slider.offers[slider.currentOfferIndex]);
    equal(cruise2, slider.offers[slider.currentOfferIndex]);

    slider.nextOffer();
    equal(slider.offers[0], slider.offers[slider.currentOfferIndex]);
    equal(excursion, slider.offers[slider.currentOfferIndex]);
});

test("Slider rotation (previous image)", function () {
    var fixture = document.getElementById("qunit-fixture");
    slider.render();
    equal(slider.offers[0], slider.offers[slider.currentOfferIndex]);
    equal(excursion, slider.offers[slider.currentOfferIndex]);

    slider.previousOffer();
    equal(slider.offers[3], slider.offers[slider.currentOfferIndex]);
    equal(cruise2, slider.offers[slider.currentOfferIndex]);

    slider.previousOffer();
    equal(slider.offers[2], slider.offers[slider.currentOfferIndex]);
    equal(cruise1, slider.offers[slider.currentOfferIndex]);

    slider.previousOffer();
    equal(slider.offers[1], slider.offers[slider.currentOfferIndex]);
    equal(holiday, slider.offers[slider.currentOfferIndex]);

});

asyncTest("Automatic rotation Test", function () {

    var fixture = document.getElementById("qunit-fixture");
    slider.render();
    equal(slider.offers[0], slider.offers[slider.currentOfferIndex]);
    equal(excursion, slider.offers[slider.currentOfferIndex]);

    setTimeout(function () {
        equal(slider.offers[1], slider.offers[slider.currentOfferIndex]);
        equal(holiday, slider.offers[slider.currentOfferIndex]);

        start();
    }, 6000);
});

asyncTest("Automatic rotation after image click Test", function () {

    var fixture = document.getElementById("qunit-fixture");
    slider.render();
    jQuery('#slider-next-arrow').click().click();
    equal(slider.offers[2], slider.offers[slider.currentOfferIndex]);
    equal(cruise1, slider.offers[slider.currentOfferIndex]);

    setTimeout(function () {
        equal(slider.offers[0], slider.offers[slider.currentOfferIndex]);
        equal(excursion, slider.offers[slider.currentOfferIndex]);
        start();
    }, 11000);
});
